﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Graphics;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using MapLibrary;
using InterfaceLibrary;
using Physics;
using Animation;
/**
 * This class is responsible for loading the level data, which includes the map data
 * and the sprite data.
 */
namespace MapLibrary
{
    public class Level
    {

        private Map mMap;
        private SpritePalette mSpriteTypes;

        [ContentSerializerIgnore]
        public Map Map
        {
            get { return mMap; }
        }

        public SpritePalette SpriteTypes
        {
            get { return mSpriteTypes; }
            set { mSpriteTypes = value; }
        }

        public string MapName { get; set; }

        public List<Sprite> Sprites
        {
            get;
            set;
        }

        public List<LevelCollisionRegion> LevelCollisionRegions
        {
            get;
            set;
        }

        public List<CollisionObject> CollisionObjectsList
        {
            get { return collisionObjects; }
            set { collisionObjects = value; }
        }

        private List<CollisionObject> collisionObjects;

        public Level()
        {
            collisionObjects = new List<CollisionObject>();
            Sprites = new List<Sprite>();
            levelCollisionRegions = new List<LevelCollisionRegion>();
            //MapName = @"testing_map_save";
        }

        public System.Collections.IEnumerable CollisionObjectsEnum()
        {
            foreach(CollisionObject obj in collisionObjects)
            {
                yield return obj;
            }

            foreach (Sprite s in Sprites)
            {
                yield return s.CollisionObject;
            }
        }

        public void AddCollisionObject(CollisionObject obj)
        {
            collisionObjects.Add(obj);
        }

        public void LoadLevel(ContentManager Content)
        {
            //for testing
            mMap = Content.Load<Map>(@"testing_map_save");
        }

        public void LoadLevel(ContentManager Content, string mapName)
        {
            MapName = mapName;
            mMap = Content.Load<Map>(mapName);
        }

        public void UpdateAnimations()
        {
            foreach (Sprite s in Sprites)
            {
                s.UpdateAnimation();
            }
        }

        public void Draw(SpriteBatch Batch, WorldViewport Viewport)
        {
            //draw the map onto the screen
            mMap.Draw(Batch, Viewport);

            foreach (Sprite s in Sprites)
            {
                s.EditorDraw(Batch, Viewport);
            }
        }

        public List<ISprite> getSpritesWithBots()
        {
            return new List<ISprite>();
        }
    }

    public class LevelContentReader : ContentTypeReader<Level>
    {
        protected override Level Read(ContentReader input, Level existingInstance)
        {
            Level level;
            if (existingInstance != null)
                level = existingInstance;
            else
                level = new Level();

            level.SpriteTypes = input.ReadObject<SpritePalette>();

            string mapName = input.ReadString();

            level.LoadLevel(input.ContentManager, mapName);

            level.Sprites = input.ReadObject<List<Sprite>>();

            level.CollisionObjectsList = input.ReadObject<List<CollisionObject>>();

            return level;
        }
    }
}
